package session.stateless.FRM;

import entity.CRM.Customer;
import entity.CRM.SaleOrder;
import entity.FRM.CashInAdvance;
import entity.FRM.CustomerInvoice;
import entity.SCM.DeliveryOrder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import session.stateless.CRM.SaleOrderBeanLocal;

@Stateless
public class CustomerInvoiceBean implements CustomerInvoiceBeanLocal {
    
    @PersistenceContext 
    private EntityManager em; 
    @EJB
    SaleOrderBeanLocal saleOrderBeanLocal;     
    @EJB
    CashInAdvanceBeanLocal cashInAdvanceBeanLocal;
    
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
    
    @Override
    public Long createCustomerInvoice_CashInAdvance(Long saleOrderId, Double cashInAdvance) {
        SaleOrder saleOrder = em.find(SaleOrder.class, saleOrderId);
        Customer customer= saleOrder.getCustomer();
        
        CustomerInvoice customerInvoice = new CustomerInvoice();
        Date date = new Date();
        customerInvoice.setCustomerInvoiceDate(dateFormat.format(date));
        customerInvoice.setCustomerInvoiceAmount(cashInAdvance);
        customerInvoice.setStateCustomerInvoice(CustomerInvoice.StateCustomerInvoice.Pending);
        customerInvoice.setSaleOrder(saleOrder);
        
        Long cashInAdvanceId= cashInAdvanceBeanLocal.createCashInAdvance_Pending(saleOrderId, cashInAdvance);
        customerInvoice.setCashInAdvance(cashInAdvanceBeanLocal.findCashInAdvanceById(cashInAdvanceId));
        
        em.persist(customerInvoice);
        
        customer.getCustomerInvoices().add(customerInvoice);
        saleOrder.getCustomerInvoices().add(customerInvoice);
        em.flush();
        
       return customerInvoice.getCustomerInvoiceId();       
    }
    
    @Override
    public Long createCustomerInvoice(Long deliveryOrderId, Double finalAmount) {
        DeliveryOrder deliveryOrder = em.find(DeliveryOrder.class, deliveryOrderId);
        SaleOrder saleOrder = deliveryOrder.getSaleOrder();
        Customer customer = saleOrder.getCustomer();
        
        CustomerInvoice customerInvoice = new CustomerInvoice();
        Date date = new Date();
        customerInvoice.setCustomerInvoiceDate(dateFormat.format(date));
        customerInvoice.setCustomerInvoiceAmount(finalAmount);
        customerInvoice.setStateCustomerInvoice(CustomerInvoice.StateCustomerInvoice.Pending);
        customerInvoice.setSaleOrder(saleOrder);
        
        em.persist(customerInvoice);
        customer.getCustomerInvoices().add(customerInvoice);
        saleOrder.getCustomerInvoices().add(customerInvoice);
        em.flush();
        
        return customerInvoice.getCustomerInvoiceId();
    }

    @Override
    public List<CustomerInvoice> getAllCustomerInvoices() {
        Query query = em.createQuery("SELECT c FROM CustomerInvoice c");
        List<CustomerInvoice> customerInvoices = query.getResultList();
        return customerInvoices;
    }

    @Override
    public CustomerInvoice findCustomerInvoice(Long customerInvoiceId) {
        CustomerInvoice temp = em.find(CustomerInvoice.class, customerInvoiceId);
        return temp;
    }

    @Override
    public void clearExpiredInvoices() {

    }

    
}
